import 'package:flutter/material.dart';

class YzText extends StatelessWidget {
  TextEditingController? controller;
  final String? text;
  final bool password;
  final bool isNum;
  final double height;
  final double width;
  final double fontSize;
  final int type;
  final int maxLine;
  final double widgetWidth;
  bool enabled;
  bool autoFocus;

  final Object? onChanged;
  Widget? iconLeft;
  Widget? iconRight;
  FocusNode? focusNode;

  YzText(
      {this.controller,
      this.text,
      this.password = false,
      this.isNum = false,
      this.height = 60,
      this.width = 1,
      this.fontSize = 16,
      this.onChanged,
      this.iconLeft,
      this.iconRight,
      this.focusNode,
      this.type = 0,
      this.maxLine = 1,
      this.widgetWidth = 150,
      this.enabled = true,
      this.autoFocus = false});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widgetWidth,
      padding: EdgeInsets.only(top: 5),
      child: TextField(
        focusNode: focusNode,
        controller: controller,
        showCursor: true,
        enabled: enabled,
        autofocus: autoFocus,
        maxLines: maxLine,
        readOnly: type == 0 ? false : true,
        obscureText: this.password,
        style: TextStyle(fontSize: fontSize),
        keyboardType: isNum ? TextInputType.number : TextInputType.text,
        decoration: InputDecoration(
            hintText: this.text,
            hintStyle: TextStyle(fontSize: 15, color: Colors.grey),
            prefixIcon: iconLeft,
            suffixIcon: iconRight,
            contentPadding: EdgeInsets.only(top: 20),
            border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(30),
                borderSide: BorderSide.none)),
//        onChanged: this.onChanged,
      ),
      height: height,
      decoration: BoxDecoration(
          border: Border(
              bottom: BorderSide(
                  width: width,
                  color: width == 0 ? Colors.transparent : Colors.black12))),
    );
  }
}

class MyTextField extends StatelessWidget {
  double? width;
  double height;
  double space;
  String hintText;
  String rightText;
  bool isShow;
  TextStyle textStyle;
  bool obscureText;
  TextInputType keyboardType;
  TextEditingController? controller;
  TextInputAction? textInputAction;
  bool isEnable;
  Function? confirm;
  Function? complete;
  Widget? iconRight;
  int? maxLines;
  String? errorText;
  bool isShowOutLine;
  EdgeInsets? contentPadding;
  EdgeInsets? margin;

  MyTextField(
      {Key? key,
      this.width,
      this.height = 90,
      this.space = 20,
      this.hintText = '',
      this.obscureText = false,
      this.keyboardType = TextInputType.text,
      this.controller,
      this.textInputAction,
      this.textStyle = const TextStyle(color: Colors.grey, fontSize: 15),
      this.isShow = false,
      this.rightText = '',
      this.isEnable = false,
      this.iconRight,
      this.maxLines = 1,
      this.errorText,
      this.isShowOutLine = true,
      this.contentPadding,
      this.margin,
      this.confirm,
      this.complete})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        width: width,
        height: height,
        margin: margin ?? EdgeInsets.zero,
        child: Row(
          children: [
            Expanded(
                child: TextField(
              controller: controller,
              obscureText: obscureText,
              obscuringCharacter: '*',
              keyboardType: keyboardType,
              textInputAction: textInputAction,
              style: textStyle,
              maxLines: maxLines,
              cursorHeight: 17,
              onEditingComplete: () {
                complete?.call();
              },
              decoration: InputDecoration(
                helperMaxLines: 3,
                contentPadding: contentPadding ?? EdgeInsets.only(left: space),
                hintText: hintText,
                hintStyle: textStyle,
                suffixIcon: iconRight,
                // errorText: errorText,
                border: OutlineInputBorder(
                    borderSide: BorderSide(
                        color:
                            isShowOutLine ? Colors.grey : Colors.transparent)),
              ),
            )),
          ],
        ));
  }
}

class MarqueeWidget extends StatefulWidget {
  final Widget? child;
  final Axis direction;
  final Duration animationDuration, backDuration, pauseDuration;
  final bool moveReverse;

  MarqueeWidget({
    @required this.child,
    this.direction = Axis.horizontal,
    this.animationDuration = const Duration(milliseconds: 3000),
    this.backDuration = const Duration(milliseconds: 3000),
    this.pauseDuration = const Duration(milliseconds: 800),
    this.moveReverse = false,
  });

  @override
  _MarqueeWidgetState createState() => _MarqueeWidgetState();
}

class _MarqueeWidgetState extends State<MarqueeWidget> {
  ScrollController scrollController = ScrollController();

  @override
  void initState() {
    // if(scrollController.positions.isNotEmpty){
    scroll();
    // }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: widget.child,
      reverse: widget.moveReverse,
      scrollDirection: widget.direction,
      controller: scrollController,
    );
  }

  void scroll() async {
    while (true) {
      try {
        await Future.delayed(widget.pauseDuration);
        await scrollController.animateTo(
            scrollController.position.maxScrollExtent,
            duration: widget.animationDuration,
            curve: Curves.easeIn);
        await Future.delayed(widget.pauseDuration);
        await scrollController.animateTo(0.0,
            duration: widget.backDuration, curve: Curves.easeOut);
      } catch (e) {}
    }
  }
}

class TImageButton extends StatelessWidget {
  String? imgPath;
  String? imgDisabledPath;
  String? text;
  double imageWidth;
  double imageHeight;
  Function? confirm;
  bool isEnabled;

  TImageButton(
      {this.imgPath,
      this.text,
      this.imageWidth = 100,
      this.imageHeight = 37,
      this.confirm,
      this.isEnabled = true,
      this.imgDisabledPath});

  @override
  Widget build(BuildContext context) {
    return
        // 设置背景颜色 默认矩形
        GestureDetector(
            onTap: () {
              if (isEnabled) {
                confirm?.call();
              } else {}
            },
            child: Container(
              width: imageWidth,
              height: imageHeight,
              padding: EdgeInsets.only(bottom: 2),
              decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage(isEnabled ? imgPath! : imgDisabledPath!),
                    fit: BoxFit.fill),
              ),
              alignment: Alignment.center,
              child: Text(
                text!,
                style: TextStyle(color: Colors.white, fontSize: 15),
              ),
              // color: Colors.transparent,
            ));
  }
}
